package com.shuguoyiyou.wechatofficialaccount.service.impl;

import com.shuguoyiyou.wechatofficialaccount.entity.po.SceneStrTemporaryQR;
import com.shuguoyiyou.wechatofficialaccount.service.MpQrcodeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by ZengJian on 2021/3/1 0001 下午 10:23
 */
@AllArgsConstructor
@Service("iMpQrcodeService")
@Slf4j
public class IMpQrcodeService implements MpQrcodeService {

    private final WxMpService wxMpService;

    @Override
    @Transactional
    public SceneStrTemporaryQR qrCodeCreateTmpTicket() {
        Long currentTime = new Date().getTime();
        System.out.println("currentTime:" + currentTime);
        String rm = RandomStringUtils.randomAlphanumeric(100);
        System.out.println("rm:" + rm);
        String sceneStr = currentTime + RandomStringUtils.randomAlphanumeric(10);
        System.out.println("sceneStr:" + sceneStr);
//        WxMpQrCodeTicket wxMpQrCodeTicket=this.wxMpService.getQrcodeService().qrCodeCreateTmpTicket(sceneStr,60);
//        System.out.println("wxMpQrCodeTicket:"+wxMpQrCodeTicket);
        String temporaryQR = "";
        try {
            WxMpQrCodeTicket wxMpQrCodeTicket = this.wxMpService.getQrcodeService().qrCodeCreateTmpTicket(sceneStr, 60);
            System.out.println("wxMpQrCodeTicket:" + wxMpQrCodeTicket);
            temporaryQR = this.wxMpService.getQrcodeService().qrCodePictureUrl(wxMpQrCodeTicket.getTicket());
        } catch (WxErrorException e) {
            System.out.println(e);
        }

        SceneStrTemporaryQR sceneStrTemporaryQR = new SceneStrTemporaryQR();
        sceneStrTemporaryQR.setSceneStr(sceneStr);
        sceneStrTemporaryQR.setTemporaryQR(temporaryQR);
        return sceneStrTemporaryQR;
    }
}
